package databasetranslation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import models.Client;
import models.Subscription;
import models.Workshop;
import database.SQLServer;

public class SubscriptionList extends DatabaseList<Subscription> {
	private WorkshopList workshops;
	private ClientList clients;
	
	/**
	 * constructor, create's a PaymentsList
	 * 
	 * @param server for the database communication	
	 * @param workshops list of workshops for searching workshops on workshop Id
	 * @param clients list of clients for searching clients on client Id
	 */
	public SubscriptionList(SQLServer server, WorkshopList workshops, ClientList clients) {
		super(server, "subscriptionlist");
		this.workshops = workshops;
		this.clients = clients;		
	}
	
	/**
	 * Return a copy of the list of Subscriptions.
	 * @return List of Subscriptions
	 */
	public List<Subscription> getSubscriptions() {
		return new ArrayList<Subscription>(getEntries());
	}
	
	/**
	 * Get list of subscriptions that belong to a workshop.
	 * 
	 * @param workshop
	 * @return list of Subscription objects (warning subscriptions are not copies)
	 */
	public List<Subscription> getSubscriptions(Workshop workshop) {
		List<Subscription> subscriptions = new ArrayList<Subscription>();
		for (Subscription subscription : getEntries()) {
			if (subscription.getWorkshop() == workshop) {
				subscriptions.add(subscription);
			}
		}
		return subscriptions;
	}
	
	/**
	 * Get list of subscriptions that belong to a client.
	 * 
	 * @param client
	 * @return list of Subscription objects (warning subscriptions are not copies)
	 */
	public List<Subscription> getSubscriptions(Client client) {
		List<Subscription> subscriptions = new ArrayList<Subscription>();
		for (Subscription subscription : getEntries()) {
			if (subscription.getClient() == client) {
				subscriptions.add(subscription);
			}
		}
		return subscriptions;
	}
	
	/**
	 * Get list of clients that are subscribed for the Workshop workshop.
	 * 
	 * @param workshop
	 * @return list of (subscribed) Clients (warning subscriptions are not copies)
	 */
	public List<Client> getSubscriptedClients(Workshop workshop) {
		List<Client> subscriptedClients = new ArrayList<Client>();
		for (Subscription subscription : getEntries()) {
			if (subscription.getWorkshop().equals(workshop)) {
				subscriptedClients.add(subscription.getClient());
			}
		}
		return subscriptedClients;
	}
	
	/**
	 * Get list of Workshops the Client client has subscribed for.
	 * @param client
	 * @return list of Workshops. (warning subscriptions are not copies)
	 */
	public List<Workshop> getWorkshopsOf(Client client) {
		List<Workshop> subscriptions = new ArrayList<Workshop>();
		for (Subscription subscription : getEntries()) {
			if (subscription.getClient().equals(client)) {
				subscriptions.add(subscription.getWorkshop());
			}
		}
		return subscriptions;
		
	}
	
	/**
	 * Returns whether the Workshop workshop has at least 1 participant.
	 * 
	 * @param workshop
	 * @return True if a participant was found.
	 */
	public boolean hasParticipants(Workshop workshop) {
		for (Subscription subscription : getSubscriptions()) {
			if (subscription.getWorkshop() == workshop) {
				// At least 1 participant
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Create a Subscription from the current selected ResultSet row
	 * 
	 * @param rs ResultSet with a selected row of a Payment
	 * @return the Subscription created from the ResultSet
	 */
	@Override
	public Subscription convertResultSetToModel(ResultSet rs) throws SQLException {
		Client subscriptionClient = clients.getEntry(rs.getInt("client"));
		Workshop subscriptionWorkshop = workshops.getEntry(rs.getInt("workshop"));
		return new Subscription(rs.getInt("id"),subscriptionClient,subscriptionWorkshop);
	}

}
